//  - main处理函数-- const具有作用域范围
const os = require('os'),
      func = require('./common.js');
var dir,app;//  全局变量
exports.main = (req,res) =>{
    var bind = req.bind;
    var tool = require(bind.dir+'/module/emma.js')
    var HTML = tool.require('html');
    dir = bind.dir;
    app = require(dir+'/config/config.js').app;
    var h = new HTML();
    h.isMobile(req,tool);
    h.load({'title':'OS || '+Math.random()*100000});
    h.appHead('<meta charset="utf-8">');
    var html = '';
    html += func.navlist(func.udata(req.url,'app'));
    html += '<br>platform：'+os.platform();
    html += '<br>type：'+os.type();
    html += '<br>uptime：'+os.uptime();
    html += '<br>release：'+os.release();
    html += '<br>tmpdir：'+os.tmpdir();
    html += '<br>totalmem：'+os.totalmem();
    html += '<br>EOL：'+os.EOL;
    html += '<br>arch：'+os.arch();
    html += '<br>endianness：'+os.endianness();
    html += '<br>freemem：'+os.freemem();
    html += '<br>homedir：'+os.homedir();
    html += '<br>hostname：'+os.hostname();
    html += '<br>loadavg：'+os.loadavg();    
    html += '<br>cpus：'+printCpu(os.cpus());
    html += '<br>networkInterfaces：'+printNet(os.networkInterfaces());

    h.appBody(html);
    res.write(h.getHtml());
    //  清除函数缓存
    func.clear(__dirname+'/common.js');
}
//  打印CPU信息
function printCpu(data)
{
    var xhtml = '<ul style="list-style:none;color:green;">';
    var cd;
    for(var k in data){
       cd = data[k];
       xhtml += `<li>型号：`+cd['model']+`</li>
       <li>速度：`+cd['speed']+`</li>
       <li>计时器
            <ul style="list-style:none;">
                <li>用户：`+cd['times']['user']+`</li>
                <li>nice：`+cd['times']['nice']+`</li>
                <li>sys：`+cd['times']['sys']+`</li>
                <li>idle：`+cd['times']['idle']+`</li>
                <li>irq：`+cd['times']['irq']+`</li>
            </ul>
       </li>
       `;
    }
    xhtml += '</ul>';
    return xhtml;
}
//  打印网络信息
function printNet(data){
    var xhtml = '<ul style="list-style:none;color:white;background:black;">';
    var cd,gcd,addr,ip;//-子对象、超子对象
    for(var k in data){
        xhtml += '<li>'+k+'<ul>';
        cd = data[k];
        for(var kk in cd){
            gcd = cd[kk];
            ip = gcd['address'];
            addr = (gcd['family'] == 'IPv4')? '<a href="http://'+ip+':'+app.port+'/node/os" target="_blank">'+ip+'</a>':ip;
            xhtml += `
            <li>
                <ul style="list-style:none;">
                    <li>IP地址: `+addr+`（`+gcd['family']+`）</li>
                    <li>网络掩码: `+gcd['netmask']+`</li>
                    <li>物理地址: `+gcd['mac']+`</li>
                    <li>链接状态: `+(gcd['internal']== true? '正在运行':'网络中断')+`</li>
                </ul>
            </li>`;
        }
        xhtml += '</ul></li>';
    }
    xhtml += '</ul>';//console.log(data);
    return xhtml;
}